package org.haredot.listener;

import org.haredot.constraint.SysConst;
import org.haredot.context.ApplicationContext;
import org.haredot.context.ClasspathApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ClassLoaderListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ApplicationContext ctx = new ClasspathApplicationContext();
        // 将 构建出来的应用上下问 存储 到 ServletContext 作用域中
        sce.getServletContext().setAttribute(SysConst.APPLICATION_CONTEXT, ctx);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // 当销毁 ServletContext 的时候，将 ctx 移除即可
        sce.getServletContext().removeAttribute(SysConst.APPLICATION_CONTEXT);
    }
}
